package com.jike.noobmoney.mvp.view.activity;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.MessageListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MessageEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2019/4/13
 * 消息界面
 */

public class MessageActivity extends BaseActivity implements IView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    ImageView empty;


    @BindView(R.id.tv_title)
    TextView tvTitle;

    private MessageListAdapter adapter;

    private List<MessageEntity.MessageBean> mData = new ArrayList<>();
    private UserPresenter userPresenter;
    private String userid;


    @Override
    protected int layoutResID() {
        return R.layout.activity_message;
    }

    @Override
    protected void initData() {
        tvTitle.setText("消息");
        userPresenter = new UserPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

        //adapter
        adapter = new MessageListAdapter(context);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        //refreshLayout.autoRefresh();  //自动刷新
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            loadData();
        });

        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
            //点击消息已读
            MessageEntity.MessageBean item = (MessageEntity.MessageBean) adapter.getItem(position);
            int msg_id = item.getMsg_id();
            int see = item.getSee();
            if (see == 0) {
                showProgress();
                if (userPresenter != null) {
                    showProgress();
                    userPresenter.setReaded(userid, String.valueOf(msg_id), ConstantValue.RequestKey.message_readed);
                }
            }
        });

        loadData();
    }

    /**
     * 获取消息
     */
    private void loadData() {
        showProgress();
        userPresenter.getMessage(userid, ConstantValue.RequestKey.message);
    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.message.equals(actionType)) {

            List<MessageEntity.MessageBean> task = (List<MessageEntity.MessageBean>) data;
            if (task != null && task.size() > 0) {
                mData.clear();
                mData.addAll(task);
            }

            if (empty != null) {
                if (mData.size() > 0) {
                    empty.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.VISIBLE);
                }
            }
            adapter.refresh(mData);
        }

        if (ConstantValue.RequestKey.message_readed.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            loadData();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
    }

    private void disLoading() {
        dismissProgress();
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }

}
